package com.ssy.lingxi.product.entity;

import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.ssy.lingxi.product.api.model.request.EnclosureUrlsRequest;
import com.ssy.lingxi.product.entity.customer.CustomerCategory;
import com.ssy.lingxi.product.entity.material.MaterialAttribute;
import com.ssy.lingxi.product.entity.material.MaterialGroup;
import com.ssy.lingxi.product.handler.converter.JpaJsonToEnclosureUrlsRequestListConverter;
import com.ssy.lingxi.product.handler.converter.JpaStringToArrayConverter;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 货品（物料）实体类
 *
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/30
 */
@Getter
@Setter
@Entity
@Table(schema = "public", name = "product_goods", indexes = {@Index(name = "product_goods_member_id_member_role_id_idx", columnList = "memberId,memberRoleId"),
        @Index(name = "product_goods_code_idx", columnList = "code"),
        @Index(name = "product_goods_name_idx", columnList = "name")})
public class Goods implements Serializable {
    private static final long serialVersionUID = 1764387721396790473L;
    /**
     * 主键id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "goods_seq")
    @SequenceGenerator(sequenceName = "product_goods_seq", name = "goods_seq", allocationSize = 1)
    private Long id;

    /**
     * 货号（物料编号）
     */
    @Column(columnDefinition = "varchar(20)", nullable = false)
    private String code;

    /**
     * 货品（物料）名称
     */
    @Column(columnDefinition = "varchar(40)", nullable = false)
    private String name;

    /**
     * 规格型号
     */
    @Column(columnDefinition = "varchar(200)", nullable = false)
    private String type;

    /**
     * 品类
     */
    @OneToOne(cascade = {CascadeType.PERSIST})
    @JoinColumn(name = "customerCategoryId", referencedColumnName = "id")
    private CustomerCategory customerCategory;

    /**
     * 品牌
     */
    @OneToOne(cascade = {CascadeType.PERSIST})
    @JoinColumn(name = "brandId", referencedColumnName = "id")
    private Brand brand;

    /**
     * 单位id
     */
    @Column(columnDefinition = "int8", nullable = false)
    private Long unitId;

    /**
     * 单位名称
     */
    @Transient
    private String unitName;

    /**
     * 成本（目录）价
     */
    @Column(columnDefinition = "decimal", nullable = false)
    private Double costPrice;

    /**
     * 物料组
     */
    @OneToOne(cascade = {CascadeType.PERSIST})
    @JoinColumn(name = "materialGroupId", referencedColumnName = "id")
    private MaterialGroup materialGroup;

    /**
     * 备注
     */
    @Column(columnDefinition = "varchar(200)")
    private String remake;

    /**
     * 是否变更
     */
    @Column(columnDefinition = "boolean")
    private Boolean change=false;

    /**
     * 物料属性
     */
    @JsonManagedReference
    @OneToMany(mappedBy = "goods", cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    private List<MaterialAttribute> materialAttributeList;

    /**
     * 物料图片
     */
    @Convert(converter = JpaStringToArrayConverter.class)
    @Column(columnDefinition = "text")
    private String[] goodsPic = new String[]{};

    /**
     * 附件信息
     */
    @Convert(converter = JpaJsonToEnclosureUrlsRequestListConverter.class)
    @Column(columnDefinition = "jsonb")
    private List<EnclosureUrlsRequest> urls = new ArrayList<>();

    /**
     * 物料货源清单
     */
    @JsonManagedReference
    @OneToMany(mappedBy = "goods", cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    private List<GoodsSupplyList> goodsSupplyLists=new ArrayList<>();

    /**
     * 外部状态
     */
    @Column
    private Integer externalState;

    /**
     * 外部工作流流程taskId
     */
    @Column
    private String externalTaskId;

    /**
     * 下一步骤，如果为0表示流程已经结束，没有下一步
     */
    @Column
    private Integer externalNextStep;

    /**
     * 内部状态
     */
    @Column
    private Integer interiorState;

    /**
     * 内部状态名称 MaterialsAddInternalStatusEnum 定义
     */
    @Transient
    private String interiorStateName;

    /**
     * 内部工作流流程taskId
     */
    @Column
    private String interiorTaskId;

    /**
     * 下一步骤，如果为0表示流程已经结束，没有下一步
     */
    @Column
    private Integer interiorNextStep;

    /**
     * 工作流key
     */
    @Column(columnDefinition = "varchar(128)"/*, nullable = false*/)
    private String processKey;

    /**
     * 导入批次
     */
    @Column(columnDefinition = "varchar(20)")
    private String batch;

    /**
     * 会员id
     */
    @Column(columnDefinition = "int8", nullable = false)
    private Long memberId;

    /**
     * 会员名称
     */
    @Column(columnDefinition = "varchar(50)", nullable = false)
    private String memberName;

    /**
     * 会员角色id
     */
    @Column(columnDefinition = "int8", nullable = false)
    private Long memberRoleId;

    /**
     * 会员角色名称
     */
    @Column(columnDefinition = "varchar(50)", nullable = false)
    private String memberRoleName;

    /**
     * 用户id
     */
    @Column(columnDefinition = "int8", nullable = false)
    private Long userId;

    /**
     * 用户名称
     */
    @Column(columnDefinition = "varchar(50)", nullable = false)
    private String userName;

    /**
     * 创建时间
     */
    @Column(columnDefinition = "int8", nullable = false)
    private Long createTime = System.currentTimeMillis();
}
